package me.pinion.model;


import com.jfinal.plugin.activerecord.TableInfoMapping;
import me.pinion.Log;
import me.pinion.model.annotation.TableBind
import me.pinion.model.book.Book
import me.pinion.model.book.Chapter;

@TableBind(table = "content_type")
public class ModelType extends XModel<ModelType>{

    public static ModelType dao = new ModelType();

    private static Map<Class<? extends XModel<?>>, ModelType> TYPE_MAP;

    static {
        TYPE_MAP = new HashMap<Class<? extends XModel<?>>, ModelType>();
    }


    public String getSimpleName(){
        String simpleName = "Something"
        switch (this.modelClass){
            case Book.class:
                simpleName = "书籍"
                break
            case Chapter.class:
                simpleName = "章节"
                break
            case Comment.class:
                simpleName = "评论"
                break
        }
        return simpleName;
    }

    public String getName(){
        return this.modelClass.name;
    }


    public ModelType getByClass(Class<? extends XModel> clazz){
        ModelType modelType;
        modelType = TYPE_MAP.get(clazz);
        if (modelType == null){
            modelType = dao.findFirst("SELECT * FROM content_type WHERE name=?", clazz.getName());
            if (modelType == null){
                modelType = new ModelType();
                modelType.set("name", clazz.getName());
                modelType.save();
            }
        }
        return modelType;
    }
    public ModelType getByClass(XModel<? extends XModel<?>> model){
        return this.getByClass(model.getClass());
    }
    public ModelType getByClass(String clazz){
        try {
            return getByClass((Class<? extends XModel>) Class.forName(clazz));
        } catch (ClassNotFoundException e) {
            Log.e(e);
            throw new RuntimeException();
        }
    }

    public Class<? extends XModel<?>> getModelClass(){
        try {
            return (Class<? extends XModel<?>>) Class.forName(this.getStr("name"));
        } catch (ClassNotFoundException e) {
            Log.e(e);
            throw new RuntimeException();
        }
    }

    public <T extends XModel<T>> T getModelDAO(){
        try {
            return (T)getModelClass().newInstance();
        } catch (InstantiationException e) {
            Log.e(e);
            throw new RuntimeException();
        } catch (IllegalAccessException e) {
            Log.e(e);
            throw new RuntimeException();
        }
    }

    public String getModelTable(){ return TableInfoMapping.me().getTableInfo(this.getModelClass()).getTableName();}



    public <T extends XModel<T>> T getModel(Object id){
        try {
            return (T) this.getModelClass().newInstance().findById(id);
        } catch (InstantiationException e) {
            Log.e(e);
            throw new RuntimeException();
        } catch (IllegalAccessException e) {
            Log.e(e);
            throw new RuntimeException();
        }
    }
    public  <T extends XModel<T>> T getModel(Class<? extends XModel<?>> clazz, Object id){ return this.getByClass(clazz).getModel(id); }
    public  <T extends XModel<T>> T getModel(String clazz, Object id){ return this.getByClass(clazz).getModel(id); }

}
